package com.shengde.jinwawaforleader.model.myJob.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseV4Fragment;
import com.shengde.jinwawaforleader.base.CommonBaseAdapter;
import com.shengde.jinwawaforleader.base.ViewHolder;
import com.shengde.jinwawaforleader.model.myJob.classList.ClassDetailsActivity;
import com.shengde.jinwawaforleader.model.myJob.classList.ClassListActivity;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProClassList;
import com.shengde.jinwawaforleader.views.ChangeClassNameDialog;

/**
 * Created by Chason on 2016/9/8.
 */

public class AllClassFragment extends BaseV4Fragment {
    private LinearLayout view;
    private ListView lv;
    private ClassListActivity activity;
    private CommonBaseAdapter<ProClassList.StudentEntity> adapter;

    @Override
    public View initView(LayoutInflater inflater) {
        view = (LinearLayout) inflater.inflate(R.layout.fragment_all_class, null);
        return view;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        activity = (ClassListActivity) getActivity();
        lv = (ListView) view.findViewById(R.id.lv);
    }

    @Override
    public void listener() {
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(activity, ClassDetailsActivity.class);
                intent.putExtra("classid", activity.student.get(position).getClassid());
                intent.putExtra("classname", activity.student.get(position).getName());
                startActivity(intent);
            }
        });
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                new AlertDialog.Builder(activity)
                        .setTitle("删除班级")
                        .setMessage("是否删除选中班级?")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                BaseProtocol.getInstance().deleteClassInfo(activity, activity.student.get(position).getClassid() + "");
                            }
                        })
                        .setNegativeButton("取消", null)
                        .show();
                return true;
            }
        });
    }

    @Override
    public void onWidgetClick(View view) {

    }

    public void paddingData() {
        if (activity.student != null) {
            if (adapter == null) {
                adapter = new CommonBaseAdapter<ProClassList.StudentEntity>(activity, activity.student, R.layout.all_class_item) {
                    @Override
                    public void convert(ViewHolder holder, final int position) {
                        TextView class_name = holder.getView(R.id.class_name);
                        TextView people_number = holder.getView(R.id.people_number);
                        class_name.setText(activity.student.get(position).getName());
                        people_number.setText("老师" + activity.student.get(position).getTnum() + "名 学生" + activity.student.get(position).getCnum() + "名");
                        final String className = class_name.getText().toString();
                        class_name.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                ChangeClassNameDialog.ShowCustomDialog(activity, className, activity.student.get(position).getClassid() + "");
                            }
                        });
                    }
                };
                lv.setAdapter(adapter);
            } else {
                adapter.resetList(activity.student);
            }
        }

    }
}